<?php
/**
 * V6
 *
 */
class data_follow_article extends data {
	public function __construct() {
		parent::__construct();
	}
	
	public function event_add ($circle_article_id) {
		$circle_article_id = intval($circle_article_id);
		
		if ($circle_article_id) {
			// 发布给圈子订阅者，circle是public。这里不用排除了，可以重复
			return $this->db->execute('
				insert into
					follow_article
				(user_id, follow_circle_pkid, circle_id, circle_article_pkid, article_id, author_id, url_id, dt)
					select
						follow_circle.user_id,
						follow_circle.id,
						follow_circle.circle_id,
						circle_article.id,
						circle_article.article_id,
						circle_article.author_id,
						circle_article.url_id,
						circle_article.dt
					from
						circle_article, circle, follow_circle
					where
						circle_article.id		= "'.$circle_article_id.'"	and
						circle.id				= circle_article.circle_id	and
						circle.privacy			= "public"					and
						follow_circle.circle_id	= circle.id
			');
		} else return false;
	}
	
	public function event_del_by_save_article ($article_id) {
		$article_id = intval($article_id);
		
		if ($article_id) {
			return $this->db->execute('
				delete from
					follow_article
				where
					article_id = "'.$article_id.'"
			');
		} else return false;
	}
	
	/**
	 * 列出关注的文章
	 *
	 * @param int $uid
	 * @param int $follow_id
	 * @param int $page				// 最小为1
	 * @param int $limit			// 最小为1
	 * @return Array
	 */
	public function ls ($uid, $follow_id=0, $page=1, $limit=20) {
		$uid		= intval($uid);
		$follow_id	= intval($follow_id);
		$page		= intval($page);
		$page		= ($page>1) ? $page : 1;
		$limit		= intval($limit);
		$limit		= ($limit>1) ? $limit : 1;
		if ($uid) {
			$execute = $this->db->execute('
				select 
					follow_article.id			AS id,
					follow_article.user_id		AS user_id,
					follow_article.dt			AS dt,
					save_article.title			AS title,
					save_article.user_id		AS author_id,
					article_url.url				AS url,
					follow_circle.nickname		AS circle_nickname
				from 
					follow_article, save_article, article_url, follow_circle
				where
					follow_article.user_id				= "'.$uid.'"										and
					'.(($follow_id) ? ' follow_article.follow_circle_pkid = "'.$follow_id.'" ' : ' 1 ').'	and
					save_article.id				= follow_article.article_id									and
					article_url.id				= save_article.url_id										and
					follow_circle.id			= follow_article.follow_circle_pkid
				order by
					follow_article.id DESC
				limit
					'.(($page-1)*$limit).', '.$limit.'
			');
			return $execute;
		} else return false;
	}
	/**
	 * 一条Follow记录
	 *
	 * @param int $uid
	 * @param int $pkid
	 * @return array
	 */
	public function one ($uid, $pkid) {
		$uid	= intval($uid);
		$pkid	= intval($pkid);
		
		if ($uid and $pkid) {
			$execute = $this->db->execute('
				select 
					follow_article.id		AS id,
					follow_article.user_id	AS user_id,
					save_article.id			AS article_id,
					save_article.id			AS content_id,
					save_article.user_id	AS author_id,
					save_article.title		AS title,
					follow_article.dt		AS dt,
					article_url.id			AS url_id,
					article_url.url			AS url
				from
					follow_article, save_article, article_url
				where
					follow_article.id		= "'.$pkid.'"					and
					follow_article.user_id	= "'.$uid.'"					and
					save_article.id			= follow_article.article_id		and
					article_url				= save_article.url_id
			');
			return $execute[0];
		} else return false;
	}
}
?>